﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;

namespace Moving03.Services
{
    /// <summary>
    /// Summary description for DatVanChuyen
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class DatVanChuyen : System.Web.Services.WebService
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        Guid idUser = new Guid();
        [WebMethod]
        public int InsertAll(string username, string password, classNoiDen noiDen, classNoiDi noiDi, classLoHang loHang, classMatHang matHang, classThongTinVanChuyen thongTinVanChuyen)
        {
            Guid idUser = CheckLogin(username, password);
            int id_NoiDen = InsertNoiDen(idUser, noiDen.Email, noiDen.DienThoai, noiDen.DiaChi1, noiDen.DiaChi2, noiDen.IdQuocGia, noiDen.MaBuuDien, noiDen.NguoiNhan, noiDen.ThanhPho, noiDen.NhaRieng);
            int id_NoiDi = InsertNoiDi(noiDi.NguoiGoi, noiDi.IdQuocGia, noiDi.DiaChi, noiDi.ThanhPho, noiDi.MaBuuDien, noiDi.Email, noiDi.DienThoai);
            int id_loHang = InsertLoHang(loHang.GiongNhau.ToString(), matHang.TenMatHang, loHang.TenLoHang, loHang.SoGoi, matHang.TrongLuong, loHang.GiaTri, loHang.TongTrongLuong);
            int kq = InsertThongTinVanChuyen(idUser, id_NoiDen, id_NoiDi, id_loHang, thongTinVanChuyen.IdPhuongThucThanhToan, thongTinVanChuyen.IdPhuongThucGoiHang, thongTinVanChuyen.ThongTinBiMat1, thongTinVanChuyen.ThongTinBiMat2, thongTinVanChuyen.TenDoanhNghiep, thongTinVanChuyen.TenLienLac, thongTinVanChuyen.TenNeuXayRaSuCo, thongTinVanChuyen.DiaChiTraHangKhiCoSuCo, thongTinVanChuyen.DungKiGui, thongTinVanChuyen.NhanEmail, thongTinVanChuyen.TacDongKhiHau);
            return kq;
        }
        public Guid Lay_IDUser(string username)
        {
            var user = context.aspnet_Users.Single(u => u.UserName == username);
            if (user != null)
            {
                return user.UserId;
            }else
                return Guid.Empty;
        }
        public Guid CheckLogin(string username, string password)
        {
            bool retVal = Membership.ValidateUser(username, password);
            if (retVal == true)
            {
                return Lay_IDUser(username);
            }
            return Guid.Empty;
        }
        public int InsertNoiDi(string HoTen, int QuocGia, string DiaChi, string ThanhPho, int MaBuuDien, string Email, string DienThoai)
        {

            try
            {
                NoiDi ndi = new NoiDi();
                ndi.NguoiGoi = HoTen;
                ndi.Id_Quocgia = QuocGia;
                ndi.Diachi = DiaChi;
                ndi.Thanhpho = ThanhPho;
                ndi.Mabuudien = MaBuuDien;
                ndi.Email = Email;
                ndi.Dienthoai = DienThoai;
                context.NoiDis.InsertOnSubmit(ndi);
                context.SubmitChanges();
                return ndi.Id_Noidi;
            }
            catch
            {
                throw new Exception();
            }
        }
        public int InsertLoHang(string GiongHayKhac, string TenMatHang, string TenLoHang, int SoGoi, double TrongLuongMoiGoi, int GiaTriLoHang, double TongKhoiLuong)
        {
            LoHang lh = new LoHang();
            if (GiongHayKhac == "1")
            {
                lh.Giong_nhau = 1;

                MatHang matHang = new MatHang();
                matHang.TenMatHang = TenMatHang;
                matHang.TrongLuong = TrongLuongMoiGoi;
                matHang.TriGia = GiaTriLoHang;
                context.MatHangs.InsertOnSubmit(matHang);
                context.SubmitChanges();
            }
            else
                lh.Giong_nhau = 0;

            lh.Ten_lo_hang = TenLoHang;
            lh.So_goi = SoGoi;
            lh.Gia_tri = GiaTriLoHang;
            lh.TongTrong_luong = TongKhoiLuong;

            context.LoHangs.InsertOnSubmit(lh);
            context.SubmitChanges();
            return lh.Id_LoHang;
        }
        public int InsertNoiDen(Guid idUser, string Email, string DienThoai, string DiaChi1, string DiaChi2, int MaQuocGia, int MaBuuDien, string NguoiNhan, string ThanhPho, int NhaRieng)
        {

            NoiDen nd = new NoiDen();
            nd.Email = Email;
            nd.Dienthoai = DienThoai;
            nd.Diachi1 = DiaChi1;
            nd.Diachi2 = DiaChi2;
            nd.Id_Quocgia = MaQuocGia;

            nd.Mabuudien = MaBuuDien;
            nd.Nguoinhan = NguoiNhan;
            nd.Thanhpho = ThanhPho;

            nd.Nharieng = NhaRieng;
            nd.IdUser = idUser;
            context.NoiDens.InsertOnSubmit(nd);
            context.SubmitChanges();
            return nd.Id_NoiDen;
            return 0;
        }
        public int InsertMatHang(int id_LoHang, string TenMatHang, float TrongLuong, float TriGia)
        {

            // insert bảng MatHang           
            MatHang mh = new MatHang();
            mh.Id_LoHang = id_LoHang;
            mh.TenMatHang = TenMatHang;
            mh.TrongLuong = TrongLuong;
            mh.TriGia = TriGia;
            context.MatHangs.InsertOnSubmit(mh);
            context.SubmitChanges();
            return 0;
        }
        public int InsertThongTinVanChuyen(Guid idUser, int idNoiDen, int idNoidi, int id_LoHang, int PhuongThucThanhToan, int PhuongThucGuiHang, string BiMat1, string BiMat2, string TenDoanhNghiep, string TenLienLac, string TenLienLacSuCo, string DiaChiTraHang, int DungKiGui, int ThongBaoQuaEmail, int TacDongKhiHau)
        {
            try
            {
                ThongTinVanChuyen ttvc = new ThongTinVanChuyen();
                ttvc.Id_PhuongThucThanhToan = PhuongThucThanhToan;
                ttvc.Id_PhuongThucGoiHang = PhuongThucGuiHang;
                ttvc.ThongTinBiMat1 = BiMat1;
                ttvc.ThongTinBiMat2 = BiMat2;
                ttvc.Id_LoHang = id_LoHang;
                ttvc.TenDoanhNghiep = TenDoanhNghiep;
                ttvc.TenLienLac = TenLienLac;
                ttvc.IdNoiden = idNoiDen;
                ttvc.IdNoiDi = idNoidi;
                ttvc.TenNeuSuCo = TenLienLacSuCo;
                ttvc.DiaChiGoiNeuSuCo = DiaChiTraHang;

                ttvc.DungKiGoi = DungKiGui;
                ttvc.NhanEmail = ThongBaoQuaEmail;
                ttvc.TacDongKhiHau = TacDongKhiHau;
                ttvc.TinhTrang = 1;
                ttvc.NgayDat = DateTime.Now;

                ttvc.Id_User = idUser;
                context.ThongTinVanChuyens.InsertOnSubmit(ttvc);
                context.SubmitChanges();
                int maVanChuyen = ttvc.Id_TTVC;
                int id_ddcn = insertDiaDiemCapNhat(maVanChuyen);
                insertDuongDiChoLoHang(maVanChuyen, id_ddcn);
                return 0;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public void insertDuongDiChoLoHang(int id_TTVC, int id_ddcn)
        {
            int id_noidi = 0;
            int id_noiden = 0;
            var query = from ttvc in context.ThongTinVanChuyens
                        join noidi in context.NoiDis on ttvc.IdNoiDi equals noidi.Id_Noidi
                        join noiden in context.NoiDens on ttvc.IdNoiden equals noiden.Id_NoiDen
                        where ttvc.Id_TTVC == id_TTVC
                        select new { id_noidi = noidi.Id_Quocgia, id_noiden = noiden.Id_Quocgia };


            foreach (var thongtin in query)
            {
                id_noidi = (int)thongtin.id_noidi;
                id_noiden = (int)thongtin.id_noiden;
            }

            var query1 = from cpddnn in context.ChiPhiDiaDiemNuocNgoais
                         join ddtg1t in context.DiaDiemTrungGian1Tuyens on cpddnn.Id_CPDDNNgoai equals ddtg1t.Id_CPDDNNgoai
                         join dd in context.DiaDiemTrungGians on ddtg1t.Id_TG equals dd.Id_DDTrungGian
                         where cpddnn.Id_QuocGia1 == id_noidi && cpddnn.Id_QuocGia2 == id_noiden
                         select dd;

            foreach (var dd in query1)
            {
                DuongDi duongdi = new DuongDi();
                duongdi.DaToiChua = 0;
                duongdi.Id_DDTGian = dd.Id_DDTrungGian;
                duongdi.Id_DDCapNhat = id_ddcn;
                context.DuongDis.InsertOnSubmit(duongdi);
                context.SubmitChanges();

            }

        }

        private int insertDiaDiemCapNhat(int id_TTVC)
        {
            DiaDiemCapNhat ddcn = new DiaDiemCapNhat();
            ddcn.Id_ThongTinVanChuyen = id_TTVC;
            context.DiaDiemCapNhats.InsertOnSubmit(ddcn);
            context.SubmitChanges();
            int id_diadiemcapnhat = ddcn.Id_DDCapNHat;
            return id_diadiemcapnhat;
        }
    }
}
